<?php
declare(strict_types = 1);

namespace app\index\service;

use think\facade\Session;
use think\facade\Config;
use think\facade\Cache;
use think\admin\Service as AdminService;

//生成分页
class PageService extends AdminService
{

    //初始化配置
    protected $table = 'Pages';//表名

    // 初始化
    protected function initialize()
    {
        //$this->config = config('excel');//调用配置文件示例
    }

    

    /**
    * 方法说明 构造分页信息
    * @author    saruri <saruri@163.com>
    * @date      2021/03/02 10:11:59  
    * @auth false
    * @menu false
    * @login false
    * @throws \think\Exception
    * @throws \think\db\exception\DataNotFoundException
    * @throws \think\db\exception\DbException
    * @throws \think\db\exception\ModelNotFoundException
    */ 
    public function  build($arr)
    {
        //do somehing
        $this->count=$arr['row'];
        $this->totalData=$arr['row']-$arr['headLine'];
        //判断总数大小 进行分页策略 获得 每页数量 去除表头
        $this->getPerPageNum($this->totalData);

        $this->totalPage=self::getTotalPage();

        $this->PerPage=self::getPerPage();

        //根据表头位置 获取数据区 数组形式 开始 结束 
        $dataZone=$this->getDataZone($arr);

        //获取具体分页 数组形式 每组的开始 结束 数字 
        $arr['detail']=$this->getDetail($dataZone);

        $data['total']=$this->count;//总数 包含表头
        $data['totalData']=$this->totalData;//总数据
        $data['totalPage']=$this->totalPage;//总页数
        $data['perNum']=$this->perNum;//单页数据
        $data['perPage']=$this->PerPage;//每页进度百分比
        $data['dataZone']=$this->dataZone;
        $data['detail']=$this->getDetail();//分页详情
        return $data;
    }


    
    /**
    * 获取数据区域
    * @author    saruri <saruri@163.com>
    * @date      2021/03/02 16:08:25  
    * @auth true
    * @menu true
    * @login true
    * @throws \think\Exception
    * @throws \think\db\exception\DataNotFoundException
    * @throws \think\db\exception\DbException
    * @throws \think\db\exception\ModelNotFoundException
    */ 
    public function  getDataZone(array $arr)
    {
        //do somehing
        $this->dataZone['start']=$arr['headLine']+1;//第一行数据的序号
        $this->dataZone['end']=$arr['row'];//总数就是最后一行
        return $this->dataZone;
        //exit("这里是:");
         
    }

    /**
    * 获取每页数量 基于策略
    * @author    saruri <saruri@163.com>
    * @date      2021/03/02 15:29:28  
    */ 
    public function  getPerPageNum(int $row)
    {
        //常用的一般小于 200条
        if ($row<=200) {
            $this->perNum= 20;
            return true;
        }
        
        if ($row>200 and $row<=10000) {
            $this->perNum= 200;
            return true;
        }

        if ($row>10000) {
            $this->perNum= 3000;
            return true;
        }

        if ($row>30000) {
            $this->perNum= 6000;
            return true;
        }

        if ($row>100000) {
            $this->perNum=10000;
            return true;
        }
    }


    /**
    * 获取每页占总数的百分比
    * @author    saruri <saruri@163.com>
    * @date      2021/03/02 16:01:25  
    */ 
    public function  getPerPage()
    {
        //do somehing
        return round($this->perNum/$this->totalData*100);
        
         
    }
    /**
    * 获取总页数
    * @author    saruri <saruri@163.com>
    * @date      2021/03/02 15:51:58  
    */ 
    public function  getTotalPage()
    {
        //do somehing
        return ceil($this->totalData/$this->perNum);
        
    }

    /**
    * 获取分页具体详情 根据已经获取的各种参数
    * @author    saruri <saruri@163.com>
    * @date      2021/03/02 16:12:15 
    */ 
    public function  getDetail()
    {
        //do somehing
        $arr=[];
        $k=0;
        for ($i=1; $i <=$this->totalPage ; $i++) { 
            # code...
            $arr[$k]['start']=1+$k*$this->perNum;
            $arr[$k]['end']=$i*$this->perNum;

            //最后一页判断
            if($i==$this->totalPage){
                $arr[$k]['end']=$this->count;  
            }
            $k++;
        }
        return $arr;
         
    }

         
   

}
